/*
** my_loption.c for my_loption in /home/chassa_m/rendu/PSU_2013_my_ls/Momo
** 
** Made by chassa_m
** Login   <chassa_m@epitech.net>
** 
** Started on  Thu Nov 28 10:44:19 2013 chassa_m
** Last update Fri Nov 29 10:55:58 2013 chassa_m
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include "../../Piscine-C-include/my.h"

struct dirent	*my_opendir(char *path, DIR *diropen)
{
  struct dirent	*dirread;

  diropen = opendir(path);
  if (diropen == NULL)
    exit(write(2, "Unable to open directory.\n", 26));
  dirread = readdir(diropen);
  if (dirread == NULL)
    exit(write(2, "Unable to read directory.\n", 26));
  return (dirread);
}

void		my_filetype(struct stat sb)
{
  char	f;

  f = S_ISREG(sb.st_mode) ? '-' : '\0';
  my_putchar(f);
  f = S_ISDIR(sb.st_mode) ? 'd' : '\0';
  my_putchar(f);
  f = S_ISCHR(sb.st_mode) ? 'c' : '\0';
  my_putchar(f);
  f = S_ISBLK(sb.st_mode) ? 'b' : '\0';
  my_putchar(f);
  f = S_ISFIFO(sb.st_mode) ? 'p' : '\0';
  my_putchar(f);
  f = S_ISLNK(sb.st_mode) ? 'l' : '\0';
  my_putchar(f);
  f = S_ISSOCK(sb.st_mode) ? 's' : '\0';
  my_putchar(f);
}

void		my_rights(struct stat sb)
{
  char	r;

  r = (sb.st_mode & S_IRUSR) ? 'r' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IWUSR) ? 'w' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IXUSR) ? 'x' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IRGRP) ? 'r' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IWGRP) ? 'w' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IXGRP) ? 'x' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IROTH) ? 'r' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IWOTH) ? 'w' : '-';
  my_putchar(r);
  r = (sb.st_mode & S_IXOTH) ? 'x' : '-';
  my_putchar(r);
  /*  ((sb.st_mode & S_IFDIR) != 0) ? my_putchar('d') : my_putchar('-');
  ((sb.st_mode & S_IRUSR) != 0) ? my_putchar('r') : my_putchar('-');
  ((sb.st_mode & S_IWUSR) != 0) ? my_putchar('w') : my_putchar('-');
  ((sb.st_mode & S_IXUSR) != 0) ? my_putchar('x') : my_putchar('-');
  ((sb.st_mode & S_IRGRP) != 0) ? my_putchar('r') : my_putchar('-');
  ((sb.st_mode & S_IWGRP) != 0) ? my_putchar('w') : my_putchar('-');
  ((sb.st_mode & S_IXGRP) != 0) ? my_putchar('x') : my_putchar('-');
  ((sb.st_mode & S_IROTH) != 0) ? my_putchar('r') : my_putchar('-');
  ((sb.st_mode & S_IWOTH) != 0) ? my_putchar('w') : my_putchar('-');
  ((sb.st_mode & S_IXOTH) != 0) ? my_putchar('x') : my_putchar('-');*/
}

char	*set_path(char *path, char *filename)
{
  char	*tmp;

  tmp = malloc((my_strlen(path) + my_strlen(filename) + 2) * sizeof(*tmp));
  if (tmp == NULL)
    exit(write(2, "PROBLEME DE MALLOC, KEUNARD.\n", 29));
  tmp = my_strdup(path); /* FONCTION SYSTEME A CORRIGER */
  my_strcat(tmp, "/");
  my_strcat(tmp, filename);
  return (tmp);
}

int	my_loption(char *path)
{
  struct stat	sb;
  DIR		*diropen;
  struct dirent *dirread;
  struct passwd *user_n;
  struct group  *grp;

  diropen = opendir(path);
  dirread = my_opendir(path, diropen);
  while ((dirread = readdir(diropen)) != NULL)
    {
      if (lstat(set_path(path, dirread->d_name), &sb) == -1)
	exit(write(2, "Unable to stat directory/file.\n", 32));
      my_filetype(sb);
      my_rights(sb);
      my_putchar('\t');
      my_printf("%u\t", sb.st_nlink);
      user_n = getpwuid(sb.st_uid);
      my_printf("%s\t", user_n->pw_name);
      grp = getgrgid(sb.st_gid);
      my_printf("%s\t", grp->gr_name);
      my_printf("%u\t", sb.st_size);
      write(1, ctime(&sb.st_mtime) + 4, 12);
      my_putchar(' ');
      my_printf("%s\n", dirread->d_name);
    }
}

int	main(int ac, char **av)
{
  if (av[1])
    my_loption(av[1]);
  else
    my_loption(".");
  return (0);
}

/* Régler le malloc de set_path. */
